package com.aaa.sbms.service;

import com.aaa.sbms.dao.NewsDao;
import com.aaa.sbms.util.FtpProperties;
import com.aaa.sbms.util.FtpUtil;
import com.aaa.sbms.util.ResultUtil;
import com.aaa.sbms.util.SessionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * fileName:NewsServiceImpl
 * description:
 * author:zz
 * createTime:2019/11/26 14:14
 * version:1.0.0
 */
@Service
//@Transactional
public class NewsServiceImpl implements NewsService {

    @Autowired(required = false)
    private NewsDao newsDao;
    @Autowired
    private FtpProperties ftpProperties;

    @Override
    public Map getList() {
        List<Map> list = newsDao.getList();
        HashMap<Object, Object> resultMap = new HashMap<>();
        resultMap.put("list",list);
        return resultMap;
    }

    @Override
    public Map getPage(Map map) {
        //开始值： begin =(pageNo-1)*pageSize
        //结束值： end  =pageNo*pageSize+1
        //获取前面输入值，计算后放入参数中
        map.put("begin",(Integer.valueOf(map.get("pageNo")+"")-1)*Integer.valueOf(map.get("pageSize")+""));
        map.put("end",Integer.valueOf(map.get("pageNo")+"")*Integer.valueOf(map.get("pageSize")+""));
        //调用 dao获取数据
        List<Map> page = newsDao.getPage(map);
        int total = newsDao.getPageCount(map);
        //合并结果
        Map resultMap = new HashMap();
        resultMap.put("page",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public Map newsAccPage(Map map) {
        //开始值： begin =(pageNo-1)*pageSize
        //结束值： end  =pageNo*pageSize+1
        //获取前面输入值，计算后放入参数中
        map.put("begin",(Integer.valueOf(map.get("pageNo")+"")-1)*Integer.valueOf(map.get("pageSize")+""));
        map.put("end",Integer.valueOf(map.get("pageNo")+"")*Integer.valueOf(map.get("pageSize")+""));
        //调用 dao获取数据
        List<Map> page = newsDao.newsAccPage(map);
        int total = newsDao.newsAccPageCount(map);
        //合并结果
        Map resultMap = new HashMap();
        resultMap.put("page",page);
        resultMap.put("total",total);
        return resultMap;
    }

    public Map newsOne(int newsid){
        HashMap<Object, Object> resultMap = new HashMap<>();
        Map one = newsDao.newsOne(newsid);
        String imagepath=null;
        if (one.get("imagepath")==null||one.get("imagepath")==""){
            resultMap.put("path","201");
        }else {
            imagepath = one.get("imagepath").toString();
            resultMap.put("path",imagepath);
        }
        resultMap.put("list",one);
        return resultMap;
    }

    @Override
    public Map newstypeList() {
        List<Map> list = newsDao.newstypeList();
        Map resultMap = new HashMap();
        resultMap.put("list",list);
        return resultMap;
    }

    public Map newstypeOne(int newstid){
        Map newstypeOne = newsDao.newstypeOne(newstid);
        HashMap<Object, Object> resultMap = new HashMap<>();
        resultMap.put("map",newstypeOne);
        return resultMap;
    }

    public Map newsAccOne(int accid){
        Map newstypeOne = newsDao.newsAccOne(accid);
        HashMap<Object, Object> resultMap = new HashMap<>();
        resultMap.put("list",newstypeOne);
        return resultMap;
    }

    @Override
    public Object add(Map map) {
        int userid = SessionUtil.getUserId();
        map.put("userid",userid);
        int i = newsDao.add(map);
        if (i>0){
            return new ResultUtil(200,"添加成功",null);
        }
        return new ResultUtil(201,"添加失败",null);
    }

    public Object addNewstype(Map map){
        int i = newsDao.addNewstype(map);
        if (i>0){
            return new ResultUtil(200,"添加成功",null);
        }
        return new ResultUtil(201,"添加失败",null);
    }

    @Override
    public Object addNewsAcc(Map map) {
        int i = newsDao.addNewsAcc(map);
        if (i>0){
            return new ResultUtil(200,"添加成功",null);
        }
        return new ResultUtil(201,"添加失败",null);
    }


    @Override
    public Object update(Map map) {
        int userid = SessionUtil.getUserId();
        map.put("userid",userid);
        int i = newsDao.update(map);
        if (i>0){
            return new ResultUtil(200,"更新成功",null);
        }
        return new ResultUtil(201,"更新失败",null);
    }

    @Override
    public Object updateNewstype(Map map) {
        int i = newsDao.updateNewstype(map);
        if (i>0){
            return new ResultUtil(200,"更新成功",null);
        }
        return new ResultUtil(201,"更新失败",null);
    }

    @Override
    public Object updateNewsAcc(Map map) {
        int i = newsDao.updateNewsAcc(map);
        if (i>0){
            return new ResultUtil(200,"更新成功",null);
        }
        return new ResultUtil(201,"更新失败",null);
    }


    @Override
    public Object delete(int newsid) {
        int i = newsDao.delete(newsid);
        if (i>0){
            return new ResultUtil(200,"删除成功",null);
        }
        return new ResultUtil(201,"删除失败",null);
    }

    @Override
    public Object deleteNewstype(int newstid) {
        int i = newsDao.deleteNewstype(newstid);
        if (i>0){
            return new ResultUtil(200,"删除成功",null);
        }
        return new ResultUtil(201,"删除失败",null);
    }

    @Override
    public Object deleteNewsAcc(int accid) {
        int i = newsDao.deleteNewsAcc(accid);
        if (i>0){
            return new ResultUtil(200,"删除成功",null);
        }
        return new ResultUtil(201,"删除失败",null);
    }
    //                                        --前台--

    @Override
    public Object newsList(Map map) {
        Map resultMap = new HashMap();//结果集
        List titles = new ArrayList();//多条新闻（title+addtime+？stick）
        int pageNo = Integer.valueOf(map.get("pageNo").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int begin = (pageNo-1)*pageSize;

        //mybatis分页器
        PageHelper.offsetPage(begin,pageSize);
        PageInfo<Map> pageInfo = new PageInfo<Map>(newsDao.newsList());//先查询全部数据
        List<Map> news = pageInfo.getList();//分页后的数据

        for (Map newOne:news){
            Map resultMap2 = new HashMap();
            int newsid = Integer.valueOf(newOne.get("newsid").toString());
            String title = "";
            if (newOne.get("stick").equals(1)){
                title = newOne.get("title").toString()+newOne.get("addtime").toString()+"【置顶】";
            }else {
                title = newOne.get("title").toString()+newOne.get("addtime").toString();
            }
            resultMap2.put("newsid",newsid);
            resultMap2.put("title",title);
            titles.add(resultMap2);
        }
        resultMap.put("list",titles);
        resultMap.put("total",pageInfo.getTotal());
        return resultMap;
    }

    @Override
    public Object newsOne2(int newsid) {
        HashMap resultMap = new HashMap();
        Map one = newsDao.newsOne2(newsid);
        resultMap.put("list",one);
        return resultMap;
    }
}